package cn.yj.core.business.service;

import cn.yj.annotation.pagehelper.page.Page;
import cn.yj.cgs.dba.entity.*;
import cn.yj.cgs.dba.entity.vo.CommentVo;
import cn.yj.cgs.dba.mapper.ArticleCommentMapper;
import cn.yj.cgs.dba.mapper.CommentMapper;
import cn.yj.cgs.dba.mapper.UserViewsAgreeMapper;
import cn.yj.common.Enums;
import cn.yj.common.UUIdUtils;
import cn.yj.commons.utils.StringUtils;
import cn.yj.core.business.service.iservice.IArticleService;
import cn.yj.core.business.service.iservice.ICommentService;
import cn.yj.core.business.service.iservice.IMessageService;
import cn.yj.core.kwfilter.KeyWorkFilterService;
import cn.yj.params.check.annotation.CheckObjectValue;
import cn.yj.params.check.annotation.KeyValue;
import cn.yj.params.check.annotation.Require;
import cn.yj.tools.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 永健
 * @since 2019-02-23
 */
@Service
@Transactional
public class CommentService extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Resource
    ArticleCommentMapper articleCommentMapper;

    @Resource
    UserViewsAgreeMapper userViewsAgreeMapper;

    @Resource
    IArticleService articleService;

    @Resource
    CustomerService customerService;

    @Resource
    private IMessageService messageService;

    @CheckObjectValue(keyValue = @KeyValue(type = Map.class, name = {"articleNo"}))
    @Override
    public Page<CommentVo> listPage(Map<String, Object> params, @Require Page<CommentVo> page) {
        return page.startPage().setRows(baseMapper.findByArticleNo(params, page));
    }

    @Override
    public List<CommentVo> findSonByArticleNoAndPId(
            @Require String articleNo, @Require Integer parentId, String userCode) {
        return baseMapper.findSonByArticleNoAndPId(articleNo, parentId, userCode);
    }


    @CheckObjectValue(keyValue = @KeyValue(type = Comment.class, name = {"articleNo", "content"}))
    @Override
    @Transactional
    public boolean reply(Comment comment) {
        // 简单过滤一下 敏感词
        checkContent(comment.getContent(), comment.getUserCode());
        String replyContent = "";
        String replyUserCode = "";
        String title = "文章评论";
        if (!StringUtils.isNull(comment.getDirectParentId())) {
            Comment replyComment = getById(comment.getDirectParentId());
            replyContent = "用户对你的评论@" + replyComment.getContent() + " 回复：";
            replyUserCode = replyComment.getUserCode();
            title = "评论回复";
            comment.setParentId(replyComment.getParentId().equals(Enums.Common.ZERO)? replyComment.getId() :
                    replyComment.getParentId());
        }

        comment.setCommentNo(UUIdUtils.getCode());

        // 插入评论
        baseMapper.insert(comment);


        // 插入中间表
        articleCommentMapper.insert(new ArticleComment(comment.getId(), comment.getArticleNo()));

        Article article = articleService.selectByNoForUpdate(comment.getArticleNo());
        article.setCommentCount(article.getCommentCount() + 1);
        articleService.updateArticleByNo(article);

        if (StringUtils.isBlank(replyContent)) {
            replyContent = "用户对文章内容@" + article.getContent() + "<br/>  评论：";
            replyUserCode = article.getUserCode();
        }
        if (title.equals("文章评论") && article.getIsAnon()) {
            return true;
        }
        messageService.notice(new Message(replyContent + comment.getContent(), title, replyUserCode));
        return true;
    }

    @Transactional
    @Override
    public boolean agreeComment(@Require String commentNo, @Require String userCode) {
        Comment comment = baseMapper.selectByIdForUpdate(commentNo);
        if (StringUtils.isNull(comment)) {
            throw new ServiceException("该条评论不存在");
        }
        baseMapper.incrOneGoodCountByNo(commentNo);

        if (userViewsAgreeMapper.selectCount(new QueryWrapper<UserViewsAgree>().lambda().eq(UserViewsAgree::getArticleNo, comment.getCommentNo()).eq(UserViewsAgree::getType, Enums.Article.USER_VIEW_TYPE.AGREE.name())) <= Enums.Common.ZERO) {
            // 插入记录
            userViewsAgreeMapper.insert(new UserViewsAgree(userCode, comment.getCommentNo(),
                    Enums.Article.USER_VIEW_TYPE.COMMENT_AGREE.name()));
            return true;
        }
        throw new ServiceException("您已经点赞过了哦～");
    }

    @Override
    public boolean deleteByNo(@Require String commentNo, @Require String userCode) {
        baseMapper.delete(updateLambda().eq(Comment::getCommentNo, commentNo).eq(Comment::getUserCode, userCode));
        return true;
    }

    @Override
    public int getAllCountNum(String articleNo) {
        return baseMapper.selectAllCommentCount(articleNo);
    }

    public void checkContent(String content, String userCode) {

        Set<String> words = KeyWorkFilterService.filter_search_out_word(content,
                customerService.getOpenIdByUserCode(userCode));
        if (!words.isEmpty()) {
            throw new ServiceException("存在敏感词,请文明评论");
        }

        List<String> str = new ArrayList<>();
        str.add("你妈逼");
        str.add("你大爷");
        str.add("傻子");
        str.add("傻逼");
        str.add("我操你");
        str.add("煞笔");
        str.add("我日");
        str.add("日我");
        str.add("贱人");
        str.add("你妈");
        str.add("妈的");
        str.add("高清");
        str.add("监控");
        str.add("偷拍");
        str.add("竞猜");
        str.add("投注");
        str.forEach(s -> {
            if (content.indexOf(s) >= 0) {
                throw new ServiceException("这位同学，你的评论已经被和谐，请文明用语呢！~~~~~~");
            }
        });

    }
}
